﻿namespace LevelEditors
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text.RegularExpressions;
    using UnityEngine;
    using UnityEngine.Serialization;
    using UnityEngine.UI;

    public class UILevelBrowser : MonoBehaviour
    {
        private List<UIElement> elements = new List<UIElement>();
        private HashSet<string> expandedPaths = new HashSet<string>();
        public static UILevelBrowser Instance;
        [FormerlySerializedAs("m_contentPrefab"), SerializeField]
        private UIElement m_elementPrefab;
        [SerializeField]
        private ToggleGroup m_toggleGroup;
        private TreeView treeView = new TreeView();

        private void Awake()
        {
            LevelEditor.Instance.onLoadLevel += () => this.Refresh();
            Instance = this;
            char[] separator = new char[] { ';' };
            this.expandedPaths = new HashSet<string>(PlayerPrefs.GetString("UILevelBrowser.expandedPaths").Split(separator));
        }

        private string GetCatetory(string fullName)
        {
            return (!fullName.Contains("/") ? "^[^/]*$" : fullName.Substring(0, fullName.LastIndexOf("/") + 1));
        }

        private UIElement GetContent(Level level)
        {
            <GetContent>c__AnonStorey3C storeyc = new <GetContent>c__AnonStorey3C {
                level = level
            };
            return this.elements.Find(new Predicate<UIElement>(storeyc.<>m__1C));
        }

        public UIElement GetContent(string name)
        {
            <GetContent>c__AnonStorey38 storey = new <GetContent>c__AnonStorey38 {
                name = name
            };
            return this.elements.Find(new Predicate<UIElement>(storey.<>m__19));
        }

        private int GetSiblingIndex(Level level)
        {
            return this.GetContent(level).transform.GetSiblingIndex();
        }

        private bool IsMatch(string input, string pattern)
        {
            try
            {
                return Regex.IsMatch(input, pattern);
            }
            catch (Exception)
            {
            }
            return false;
        }

        private void OnDestroy()
        {
            PlayerPrefs.SetString("UILevelBrowser.expandedPaths", string.Join(";", this.expandedPaths.ToArray<string>()));
        }

        private void OnReorder()
        {
            LevelEditor.Instance.levels.Sort((a, b) => this.GetSiblingIndex(a).CompareTo(this.GetSiblingIndex(b)));
            this.Start();
        }

        private void Refresh()
        {
            <Refresh>c__AnonStorey39 storey = new <Refresh>c__AnonStorey39 {
                <>f__this = this,
                nodes = this.treeView.GetTreeNodes().ToList<TreeView.Node>()
            };
            UIUtility.RefreshElements<UIElement>(this.elements, storey.nodes.Count, this.m_elementPrefab, this.m_toggleGroup.transform, new Action<UIElement, int>(storey.<>m__1A));
        }

        private void Start()
        {
            foreach (Level level in LevelEditor.Instance.levels)
            {
                this.treeView.AddPath(level.name);
            }
            this.treeView.GetAllNodes().Foreach<TreeView.Node>((Action<TreeView.Node>) (x => (x.expanded = this.expandedPaths.Contains(x.path))));
            this.Refresh();
        }

        [CompilerGenerated]
        private sealed class <GetContent>c__AnonStorey38
        {
            internal string name;

            internal bool <>m__19(UIElement x)
            {
                return (x.name == this.name);
            }
        }

        [CompilerGenerated]
        private sealed class <GetContent>c__AnonStorey3C
        {
            internal Level level;

            internal bool <>m__1C(UIElement c)
            {
                return (c.target == this.level);
            }
        }

        [CompilerGenerated]
        private sealed class <Refresh>c__AnonStorey39
        {
            internal UILevelBrowser <>f__this;
            internal List<TreeView.Node> nodes;

            internal void <>m__1A(UIElement c, int i)
            {
                <Refresh>c__AnonStorey3A storeya;
                storeya = new <Refresh>c__AnonStorey3A {
                    <>f__ref$57 = this,
                    c = c,
                    node = this.nodes[i],
                    level = LevelEditor.Instance.levels.FirstOrDefault<Level>(new Func<Level, bool>(storeya.<>m__1D))
                };
                storeya.c.group = this.<>f__this.m_toggleGroup;
                string str = string.Empty;
                if (storeya.node.children.Count > 0)
                {
                    str = !storeya.node.expanded ? "+" : "-";
                }
                storeya.c.label = new string('\t', storeya.node.indentLevel) + str + storeya.node.name;
                storeya.c.onRightClick = new Action(storeya.<>m__1E);
                if (storeya.level != null)
                {
                    storeya.c.name = storeya.level.name;
                    storeya.c.isOn = (LevelEditor.Instance.curLevel != null) && (LevelEditor.Instance.curLevel.name == storeya.c.name);
                    storeya.c.onSelected = new Action(storeya.<>m__1F);
                    storeya.c.index = i;
                    storeya.c.target = storeya.level;
                }
            }

            private sealed class <Refresh>c__AnonStorey3A
            {
                internal UILevelBrowser.<Refresh>c__AnonStorey39 <>f__ref$57;
                internal UIElement c;
                internal Level level;
                internal TreeView.Node node;

                internal bool <>m__1D(Level x)
                {
                    return (x.name == this.node.path);
                }

                internal void <>m__1E()
                {
                    this.node.expanded = !this.node.expanded;
                    this.<>f__ref$57.<>f__this.Refresh();
                    if (this.node.expanded)
                    {
                        this.<>f__ref$57.<>f__this.expandedPaths.Add(this.node.path);
                    }
                    else
                    {
                        this.<>f__ref$57.<>f__this.expandedPaths.Remove(this.node.path);
                    }
                    if (this.level != null)
                    {
                        <Refresh>c__AnonStorey3B storeyb = new <Refresh>c__AnonStorey3B {
                            <>f__ref$58 = this
                        };
                        UIGenericMenu.Clear();
                        storeyb.folderName = Path.GetDirectoryName(this.level.asset.dataFileName);
                        if (Directory.Exists(storeyb.folderName))
                        {
                            UIGenericMenu.Add("打开文件夹", false, new Action(storeyb.<>m__20));
                        }
                        UIGenericMenu.Show();
                    }
                }

                internal void <>m__1F()
                {
                    if ((LevelEditor.Instance.curLevel == null) || (LevelEditor.Instance.curLevel.name != this.c.name))
                    {
                        LevelEditor.Instance.LoadLevel(this.c.name);
                    }
                }

                private sealed class <Refresh>c__AnonStorey3B
                {
                    internal UILevelBrowser.<Refresh>c__AnonStorey39.<Refresh>c__AnonStorey3A <>f__ref$58;
                    internal string folderName;

                    internal void <>m__20()
                    {
                        Application.OpenURL(this.folderName);
                    }
                }
            }
        }
    }
}

